package com.examiner.dao;

import com.examiner.bean.Manager;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

/**
 * 管理员 dao 层
 * 自定义接口需要继承
 * 		JpaRepository<实体类的类型,实体类中的主键的类型>：基本的 增删改查、分页、排序操作
 * 		JpaSpecificationExecutor<实体类的类型>：动态sql
 */
public interface ManagerDao extends JpaRepository<Manager,Integer> , JpaSpecificationExecutor<Manager> {

    /* 根据userName查询管理员 */
    //@Query(value = "select * from manager where manager_user_name=:managerUserName",nativeQuery = true)
    Manager findManagerByUserName(String managerUserName);


    /**
     * 修改密码
     * @param managerId
     * @param newPassword
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update Manager set password=:newPassword where id=:managerId",nativeQuery = true)
    int updatePassword(@Param("managerId")Integer managerId,
                               @Param("newPassword") String newPassword);

    /**
     * 查询分页总条数
     * @param query
     * @return
     */
    @Query(value = "select count(id) from manager where user_name like %?1%",nativeQuery = true)
    int getTotal(String query);
}
